<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col>
        <el-table v-loading="loading" :data="userList">
          <el-table-column type="selection" width="45" align="center" />
          <el-table-column label="编号" width="80" align="center" prop="empId" />
          <el-table-column
            label="用户名称"
            align="center"
            prop="empName"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="用户昵称"
            align="center"
            prop="nickName"
            :show-overflow-tooltip="true"
          />
          <el-table-column min-width="300px" label="Title">
            <template slot-scope="scope">
              <router-link :to="'/user/detail/'+scope.row.empId" class="link-type">
                <span>{{ scope.row.empName }}</span>
              </router-link>
            </template>
          </el-table-column>
          <el-table-column label="部门" align="center" prop="deptName" :show-overflow-tooltip="true" />
          <el-table-column label="手机号码" align="center" prop="phone" width="120" />
          <el-table-column label="状态" width="50" align="center">
            <template slot-scope="scope">
              <el-switch v-model="scope.row.status" active-value="0" inactive-value="1" />
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createdAt" width="165">
            <template slot-scope="scope">
              <span>{{ scope.row.empId }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="220"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                @click="userUpdate(scope.row)"
              >{{ $t('table.edit') }}</el-button>
              <!-- <el-button
                v-permisaction="['system:sysuser:edit']"
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="userUpdate(scope.row)"
              >修改</el-button>-->
              <!-- <el-button
                v-if="scope.row.userId !== 1"
                v-permisaction="['system:sysuser:remove']"
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除</el-button>
              <el-button
                v-permisaction="['system:sysuser:resetPassword']"
                size="mini"
                type="text"
                icon="el-icon-key"
                @click="handleResetPwd(scope.row)"
              >重置</el-button>-->
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageIndex"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户昵称" prop="empName">
              <el-input v-model="form.empName" placeholder="请输入用户昵称" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="归属部门" prop="deptId">
              <treeselect
                v-model="form.deptId"
                :options="deptOptions"
                :normalizer="normalizer"
                placeholder="请选择归属部门"
              />
            </el-form-item>
          </el-col>-->
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phone">
              <el-input placeholder="请输入手机号码" maxlength="11" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input placeholder="请输入邮箱" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户名称" prop="username">
              <el-input placeholder="请输入用户名称" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item v-if="form.userId == undefined" label="用户密码" prop="password">
              <el-input v-model="form.password" placeholder="请输入用户密码" type="password" />
            </el-form-item>
          </el-col>-->
          <!-- <el-col :span="12">
            <el-form-item label="用户性别">
              <el-select v-model="form.sex" placeholder="请选择">
                <el-option
                  v-for="dict in sexOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>-->
          <!-- <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{ dict.dictLabel }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>-->

          <!-- <el-col :span="12">
            <el-form-item label="岗位">
              <el-select v-model="form.postId" placeholder="请选择" @change="$forceUpdate()">
                <el-option
                  v-for="item in postOptions"
                  :key="item.postId"
                  :label="item.postName"
                  :value="item.postId"
                  :disabled="item.status == 1"
                />
              </el-select>
            </el-form-item>
          </el-col>-->
          <!-- <el-col :span="12">
            <el-form-item label="角色">
              <el-select placeholder="请选择" @change="$forceUpdate()">
                <el-option
                  v-for="item in roleOptions"
                  :key="item.roleId"
                  :label="item.roleName"
                  :value="item.roleId"
                  :disabled="item.status == 1"
                />
              </el-select>
            </el-form-item>
          </el-col>-->
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getUserList, getUser } from '@/api/user'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'User',
  components: { Pagination },
  data() {
    return {
      // 遮罩层
      loading: true,
      total: 0,
      queryParams: {
        pageIndex: 1,
        pageSize: 10
      },
      userList: null,
      form: {},
      open: false,
      title: '',
      // 表单校验
      rules: {
        username: [
          { required: true, message: '用户名称不能为空', trigger: 'blur' }
        ],
        nickName: [
          { required: true, message: '用户昵称不能为空', trigger: 'blur' }
        ],
        deptId: [
          { required: true, message: '归属部门不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '用户密码不能为空', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱地址不能为空', trigger: 'blur' },
          {
            type: 'email',
            message: "'请输入正确的邮箱地址",
            trigger: ['blur', 'change']
          }
        ],
        phone: [
          { required: true, message: '手机号码不能为空', trigger: 'blur' },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: '请输入正确的手机号码',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true
      getUserList(this.queryParams).then(response => {
        console.log(response)
        this.userList = response.data
        this.total = response.total
        this.loading = false
      })
    },
    reset() {
      this.form = {
        empId: undefined,
        empName: undefined,
        password: undefined
      }
    },
    userUpdate(row) {
      this.reset()
      const userId = row.empId
      getUser(userId).then(response => {
        console.log(response)
        this.form = response.data
        // this.postOptions = response.posts
        // this.roleOptions = response.roles
        // this.form.postIds = response.postIds[0]
        // this.form.roleIds = response.roleIds[0]
        this.open = true
        this.title = '修改用户'
        this.form.password = ''
      })
    },
    cancel() {
      this.open = false
      this.reset()
    },
    submitForm: function() {}
  }
}
</script>
<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
